package com.base.cn.platform.os.common;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
@Component
public class CommonDownloadTemplate {

    @Autowired
    private   Environment environment;
    public ResponseEntity<byte[]> downloadTemplate(String urls,String fileName ){

        String url =  environment.getProperty("static.path")+urls;
        RestTemplate rest = new RestTemplate();
        HttpEntity requestEntity = new HttpEntity(null,  new LinkedMultiValueMap<>());
        ResponseEntity<byte[]> exchange = rest.exchange(url, HttpMethod.GET, requestEntity, byte[].class);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<>(exchange.getBody(),headers, HttpStatus.OK);
    }
}

